<?php
	defined( '_JEXEC' ) or die( 'Restricted access' );
	jimport( 'joomla.application.component.model' );
	require_once(JPATH_COMPONENT.DS.'libraries'.DS.'core.php' );
	
	class ModelHizkimizkiLanguageList extends JModel {
		var $_languages=null;
		var $_words=null;
		
		function __construct()
		{
			parent::__construct();
		}
		
		function getAllLanguages(){
			if($this->_languages==null){
				$db =& JFactory::getDBO();
				$query = "SELECT *
							FROM `#__hizkimizki_languages`
							ORDER BY `#__hizkimizki_languages`.`id` ASC";
				$db->setQuery( $query );
				$languages=$db->loadObjectList();
				$this->_languages=array();
				foreach($languages as $language){
					$languageObj=new Language($language->id);
					$languageObj->_author = $language->authorName;
					$languageObj->_active = $language->active;
					$languageObj->_date = $language->modDate;
					$this->_languages[] =$languageObj;
				}
			}
			return $this->_languages;
		}

		function getLang($iso_code){
			$db =& JFactory::getDBO();
			$query = "SELECT * FROM #__hizkimizki_languages
						WHERE #__hizkimizki_languages.id='$iso_code'";
			$db->setQuery( $query );
			$language=$db->loadObject();
			$languageObj=new Language($language->id);
			$languageObj->_author = $language->authorName;
			$languageObj->_active = $language->active;
			$languageObj->_email = $language->email;
			$languageObj->_date = $language->modDate;
			
			return $languageObj;
		}

		function getPLang($iso_code){
			$db =& JFactory::getDBO();
			$query = "SELECT * FROM #__hizkimizki_prop_languages
						WHERE #__hizkimizki_prop_languages.id='$iso_code'";

			$db->setQuery( $query );
			$language=$db->loadObject();
			$languageObj=new Language($language->id);
			$languageObj->_author = $language->author;
			$languageObj->_email = $language->email;
			$languageObj->_name = $language->name;
			$languageObj->_active = $language->active;
			$languageObj->_date = $language->date;
			
			return $languageObj;
		}

		function getLanguages(){
			if($this->_languages==null){
				$db =& JFactory::getDBO();
				$query = "SELECT *
							FROM `#__hizkimizki_languages`
							WHERE `#__hizkimizki_languages`.`active`=1
							ORDER BY `#__hizkimizki_languages`.`id` ASC";
				$db->setQuery( $query );
				$languages=$db->loadObjectList();
				$this->_languages=array();
				foreach($languages as $language){
					$languageObj=new Language($language->id);
					$languageObj->_author = $language->authorName;
					$languageObj->_active = $language->active;
					$languageObj->_date = $language->modDate;
					$languageObj->_email = $language->email;
					$this->_languages[] =$languageObj;
				}
			}
			return $this->_languages;
		}

		function getLangWords($langOri){
						
			if($langOri != null){
				$db =& JFactory::getDBO();
				$query = "SELECT *
							FROM #__hizkimizki_words
							WHERE #__hizkimizki_words.idLanguage='$langOri'";
				$db->setQuery( $query );
				$words=$db->loadObjectList();
				$this->_words=array();
				
				foreach ($words as $word) {
					
					$wordObj = new Word($word->id);
					$wordObj->_word = $word->word;
					$wordObj->_wordNum = $word->wordNum;
					$wordObj->_language = $word->idLanguage;
					$this->_words[] = $wordObj;
				}
				
			}
			return $this->_words;
		}

		function getPLangWords($langOri){
						
			if($langOri != null){
				$db =& JFactory::getDBO();
				$query = "SELECT *
							FROM #__hizkimizki_prop_words
							WHERE #__hizkimizki_prop_words.idLanguage='$langOri'";
				$db->setQuery( $query );
				$words=$db->loadObjectList();
				$this->_words=array();
				
				foreach ($words as $word) {
					
					$wordObj = new Word($word->id);
					$wordObj->_word = $word->word;
					$wordObj->_wordNum = $word->wordNum;
					$wordObj->_language = $word->idLanguage;
					$this->_words[] = $wordObj;
				}
				
			}
			
			return $this->_words;
		}

		function getProposedLangs(){
			if($this->_languages==null){
				$db =& JFactory::getDBO();
				$query = "SELECT *
							FROM `#__hizkimizki_prop_languages`
							ORDER BY `#__hizkimizki_prop_languages`.`active` ASC";
				$db->setQuery( $query );
				$languages=$db->loadObjectList();
				$prop_languages=array();
				foreach($languages as $language){
					$languageObj=new Language($language->id);
					$languageObj->_author = $language->author;
					$languageObj->_email = $language->email;
					$languageObj->_name = $language->name;
					$languageObj->_active = $language->active;
					$languageObj->_date = $language->date;
					$prop_languages[] =$languageObj;
				}
			}
			return $prop_languages;
		}

		function addTranslation(){

			$reflang = JRequest::getString('reflang',null);
			$newcode = JRequest::getString('iso', null);
			$newLang = JRequest::getString('language', null);
			$author = JRequest::getString('name', null);
			$email = JRequest::getString('email', null);
			$result = true;

			if ($reflang != null 
					&& $newcode != null
					&& $newLang != null
					&& $author != null
					&& $email != null) {

				$active = $this->managePLangWords("insert", $reflang, $newcode);
				/*
					Section to create the new proposed language
				*/
				$plang = new stdClass();
				$plang->id = $newcode;
				$plang->name = $newLang;
				$plang->author = $author;
				$plang->email = $email;
				$plang->active = $active;
				$plang->date = null;

				$db =& JFactory::getDBO();
				
				if(!$db->insertObject( '#__hizkimizki_prop_languages', $plang, 'id' )){
					echo $db->stderr();
					$result = false;
				}
				
			}else
				$result = false;

			return $result;
		}

		function stateTranslation(){
			$act = JRequest::getString('act',null);
			$code = JRequest::getString('code', null);
			$result = false;
			if($act != null && $code != null){
				$lang = null;
				$result = true;

				if($act == "accept"){
					$lang = $this->getPLang($code);
					$lang->setActive(1);
					$words = $this->getPLangWords($code);
					if(!$this->manageLang('insert', $lang))
						$result = false;
					if(!$this->manageLangWords('insert', null, $code, $words, null))
						$result = false;
					if(!$this->deletePLangWords($code))
						$result = false;
					if(!$this->deletePLang($code))
						$result = false;
					if($result)
						$this->editLanguageFile($lang->getId(), $lang->getName());

				}else if($act == "active"){
					$lang = $this->getLang($code);
					$lang->setActive(1);
					if(!$this->manageLang('update', $lang))
						$result = false;
				}else if($act == "inactive"){
					$lang = $this->getLang($code);
					$lang->setActive(0);
					if(!$this->manageLang('update', $lang))
						$result = false;
				}
			}
			return $result;
		}

		function updateTranslation(){
			$type = JRequest::getString('type',null);
			if($type != null){
				$reflang = JRequest::getString('reflang',null);
				$iso = JRequest::getString('iso', null);
				$code = JRequest::getString('lang', null);
				$lname = JRequest::getString('language', null);
				$author = JRequest::getString('name', null);
				$email = JRequest::getString('email', null);
				$lang = null;
				$result = true;

				if($type == "prop"){
					$lang = $this->getPLang($code);
					$lang->setName($lname);

				}else if($type != null && $type == "accepted"){
					$lang = $this->getLang($code);
				}else
					$result = false;

				if($result){
					$lang->setAuthor($author);
					$lang->setEmail($email);
					$lang->setDate(null);
					if($type == "prop"){
						$lang->setActive($this->managePLangWords('update', $reflang, $code, $iso));
						$result = $this->managePLang('update', $lang, $iso);
					}
					else if($type == "accepted"){
						$this->manageLangWords('update', $reflang, $code, null, $iso);
						$result = $this->manageLang('update', $lang, $iso);
					}
				}

				return $result;
			}
		}

		function managePLang($action, $lang, $iso){
			$result = true;
			$db =& JFactory::getDBO();

			if($action == 'insert'){
				if(!$db->insertObject( '#__hizkimizki_prop_languages', $lang, 'id' )){
					echo $db->stderr();
					$result = false;
				}
			} else if($action == 'update'){
				$sql = "UPDATE #__hizkimizki_prop_languages 
						SET id='$iso',
							name='$lang->_name', 
							author='$lang->_author', 
							email='$lang->_email', 
							active='$lang->_active',
							date=null 
						WHERE id='$lang->_id'";
				$db->setQuery($sql);
		
				if(!$db->query()){
					echo $db->stderr();
					$result =  false;
				}
			}
			
			return $result;
		}

		function manageLang($action, $lang, $iso){
			$result = true;
			$db =& JFactory::getDBO();
			if($action == 'insert'){
				$sql = "INSERT INTO #__hizkimizki_languages 
						(id, email, modDate, authorName, active) 
						VALUES ('$lang->_id', 
								'$lang->_email', 
								null, 
								'$lang->_author', 
								'$lang->_active')";
				$db->setQuery($sql);
		
				if(!$db->query()){
					echo $db->stderr();
					$result =  false;
				}
			} else if($action == 'update'){
				$sql = "UPDATE #__hizkimizki_languages 
						SET id='$iso'
							authorName='$lang->_author', 
							email='$lang->_email', 
							active='$lang->_active',
							modDate=null 
						WHERE id='$lang->_id'";
				$db->setQuery($sql);
		
				if(!$db->query()){
					echo $db->stderr();
					$result =  false;
				}
			}

			return $result;
		}

		function managePLangWords($action, $reflang, $iso_code, $niso){
			$db =& JFactory::getDBO();
			
			$query = "SELECT *
						FROM #__hizkimizki_words
						WHERE #__hizkimizki_words.idLanguage='$reflang'";
			
			$db->setQuery( $query );
			$words=$db->loadObjectList();

			$active = "complete";

			foreach ($words as $word) {
				$pword = new stdClass();
				$pword->id = null;
				$pword->idLanguage =  $iso_code;
				$pword->wordNum = $word->wordNum;
				$pword->date = null;

				$tvalue = JRequest::getString("word_$word->wordNum", null);
				$pword->word = mysql_real_escape_string($tvalue);

				if($tvalue == null || $tvalue == ''){
					$active = "incomplete";
				}
				
				if($action == 'insert'){
					if(!$db->insertObject( '#__hizkimizki_prop_words', $pword, 'id' )){
						echo $db->stderr();
						$result = false;
					}
				} else if($action == 'update'){
					$sql = "UPDATE #__hizkimizki_prop_words 
							SET idLanguage='$niso', word='$pword->word', date=null
							WHERE wordNum=$pword->wordNum AND idLanguage='$pword->idLanguage'";
					if(strrpos($pword->word, "'")){
						print($sql);
					}
					$db->setQuery($sql);
					if(!$db->query()){
						echo $db->stderr();
						$result =  false;
					}
				}
					
			}
			return $active;
		}

		function manageLangWords($action, $reflang, $iso_code, $lwords, $niso){
			$db =& JFactory::getDBO();

			$active = "complete";

			if($action == 'update'){
				$query = "SELECT *
						FROM #__hizkimizki_words
						WHERE #__hizkimizki_words.idLanguage='$reflang'";
			
				$db->setQuery( $query );
				$words=$db->loadObjectList();
				foreach ($words as $word) {
					$pword = new stdClass();
					$pword->id = null;
					$pword->idLanguage =  $iso_code;
					$pword->wordNum = $word->wordNum;
					$pword->date = null;

					$tvalue = JRequest::getString("word_$word->wordNum", null);
					$pword->word = $tvalue;
					if($tvalue == null || $tvalue == ''){
						$active = "incomplete";
					}
					
					$sql = "UPDATE #__hizkimizki_words 
							SET idLanguage='$niso', word='$pword->word', modDate=null 
							WHERE wordNum=$pword->wordNum AND idLanguage='$pword->idLanguage'";
					$db->setQuery($sql);
					if(!$db->query()){
						echo $db->stderr();
						$result =  false;
					}					
				}
			}else if($action == 'insert'){
				foreach ($lwords as $word) {
										
					$sql = "INSERT INTO #__hizkimizki_words 
						(id, wordNum, idLanguage, word, modDate) 
						VALUES (null, 
								'$word->_wordNum', 
								'$word->_language', 
								'$word->_word', 
								null)";
					$db->setQuery($sql);
			
					if(!$db->query()){
						echo $db->stderr();
						$result =  false;
					}
				}
			}

			return $active;
		}

		function deletePLang($iso){
			$db =& JFactory::getDBO();
			
			$result = true;
			$query = "DELETE
						FROM #__hizkimizki_prop_languages
						WHERE #__hizkimizki_prop_languages.id='$iso'";
			
			$db->setQuery( $query );
			if(!$db->query()){
				echo $db->stderr();
				$result =  false;
			}

			return $result;			
		}

		function deletePLangWords($iso){
			$db =& JFactory::getDBO();

			$result = true;
			$query = "DELETE
						FROM #__hizkimizki_prop_words
						WHERE #__hizkimizki_prop_words.idLanguage='$iso'";
			
			$db->setQuery( $query );
			if(!$db->query()){
				echo $db->stderr();
				$result =  false;
			}

			return $result;			
		}

		function editLanguageFile($iso_code, $lname){
			$path = 'components'.DS.'com_hizkimizki'.DS.'language'.DS.'en-GB'.DS.'en-GB.com_hizkimizki.ini';
			$file = fopen($path, 'a');
			$line = "LANG_". strtoupper($iso_code)."=\"".$lname."\"\n";
			fwrite($file, $line);
			fclose($file);
		}
	}
?>